package com.ocean.admin.controller;


import com.ocean.admin.core.annotation.Log;
import com.ocean.admin.core.annotation.enums.BusinessType;
import com.ocean.admin.model.dto.menu.SysMenuQueryDTO;
import com.ocean.admin.model.dto.menu.SysMenuSaveDTO;
import com.ocean.admin.model.dto.menu.SysMenuUpdateDTO;
import com.ocean.admin.model.vo.menu.SysMenuRuleVo;
import com.ocean.admin.model.vo.menu.SysMenuVo;
import com.ocean.admin.service.ISysMenuService;
import com.ocean.core.util.response.ResultSet;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Tag(name = "菜单管理")
@RestController
@RequestMapping("/admin/sys-menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * 获取菜单列表
     *
     * @param queryDTO 查询条件
     * @return 结果
     */
    @Operation(summary = "查询菜单树")
    @PreAuthorize("@ss.hasPermission('system:menu:listTree')")
    @PostMapping(value = "listTree")
    public ResultSet<List<SysMenuVo>> listTree(@RequestBody(required = false) SysMenuQueryDTO queryDTO) {
        return ResultSet.ok(iSysMenuService.listTree(queryDTO));
    }

    /**
     * 根据角色查询菜单
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Parameter(name = "roleId", description = "角色ID", required = true)
    @Operation(summary = "根据角色查询菜单")
    @GetMapping(value = "roleMenu/{roleId}")
    public ResultSet<SysMenuRuleVo> roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        return ResultSet.ok(iSysMenuService.roleMenuTreeSelect(roleId));
    }

    /**
     * 新增菜单
     *
     * @param saveDTO 菜单信息
     * @return 结果
     */
    @Operation(summary = "新增菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:saveMenu')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "saveMenu")
    public ResultSet<String> saveMenu(@Validated @RequestBody SysMenuSaveDTO saveDTO) {
        return ResultSet.ok(iSysMenuService.saveMenu(saveDTO));
    }

    /**
     * 修改菜单
     *
     * @param updateDTO 菜单信息
     * @return 结果
     */
    @Operation(summary = "修改菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:updateMenu')")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateMenu")
    public ResultSet<String> updateMenu(@Validated @RequestBody SysMenuUpdateDTO updateDTO) {
        return ResultSet.ok(iSysMenuService.updateMenu(updateDTO));
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Operation(summary = "删除菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:removeMenu')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "removeMenu/{menuId}")
    public ResultSet<String> removeMenu(@PathVariable("menuId") Long menuId) {
        return ResultSet.ok(iSysMenuService.removeMenu(menuId));
    }
}
